const app = getApp();
var utils = require('../../untils/util.js');
Page({
  data: {
    checkStatus: 0,
    reqData: {
      "xgmember.nickName": "",
      "xgmember.phone": "",
      "xgmember.code": "",
      "xgmember.yzcode": "",
      "xgmember.passWord": "",
      "xgmember.idCard": "",
      "xgmember.sex": 402,
     "cuuid": "",
      "xuuid": "",
      "code":"",
      // "openId":'',
      // "openId":''
    },
    placeList: [],
    objectPlaceList: [],
    cregions: '',//城市
    xregions: '',//地区，
    smsAvailable: true,
    smsSeconds: 60,
    disabled: false,
  },
  onLoad: function () {
    this.initPage();
  },
  initPage: function () {
    var _this = this;
    app.doReqSyn(app.globalData.COMMON.ADDRLIST, {
      parentId: 2
    }, 'get').then(function (res) {
      var data = res.data;
      if (data.code == 200) {
        var cityList = data.obj[2];
        var cityListTemp = [];
        for (let i of cityList) {
          cityListTemp.push(i.name);
        }
        _this.setData({
          placeList: [cityListTemp],
          objectPlaceList: [cityList]
        })
        return { cityList: cityList, cityListTemp: cityListTemp };
      }
    }).then(function (res) {
      let cityList = res.cityList;
      let cityListTemp = res.cityListTemp;
      app.doReqSyn(app.globalData.COMMON.ADDRLIST, {
        parentId: 5
      }, 'get').then(function (res) {
        let data = res.data;
        if (data.code == 200) {
          var areaList = data.obj[5];
          var areaListTemp = [];
          for (let i of areaList) {
            areaListTemp.push(i.name);
          }
          _this.setData({
            placeList: [cityListTemp, areaListTemp],
            objectPlaceList: [cityList, areaList]
          })
        }
      });
    });

  },
  whenCityChange: function (e) {
    var _this = this;
    var colum = e.detail.column;
    var cityIndex = e.detail.value;
    var cityId = _this.data.objectPlaceList[0][cityIndex].id;
    if (colum == 0) {
      app.doReq(app.globalData.COMMON.ADDRLIST, {
        parentId: cityId
      }, 'get', function (res) {
        let data = res.data;
        if (data.code == 200) {
          let areaList = data.obj[cityId];
          let areaListTemp = [];
          for (let i of areaList) {
            areaListTemp.push(i.name);
          }
          _this.setData({
            placeList: [_this.data.placeList[0], areaListTemp],
            objectPlaceList: [_this.data.objectPlaceList[0], areaList]
          })
        }
      });
    }
  },
  whenChage: function (e) {
    var v = e.detail.value;//数组
    var nodeType = e.currentTarget.dataset.nodetype;
    if (nodeType == 'region') {
      let v1 = v[0] != null ? v[0] : 0;
      let v2 = v[1] != null ? v[1] : 0;
      this.setData({
        cregions: this.data.objectPlaceList[0][v1].name,
        xregions: this.data.objectPlaceList[1][v2].name
      });
      this.data.reqData.cuuid = this.data.objectPlaceList[0][v1].id
      this.data.reqData.xuuid = this.data.objectPlaceList[1][v2].id;
    }
  },
  whenInput: function (e) {
    var v = e.detail.value;
    var nodeType = e.currentTarget.dataset.nodetype;
    if (nodeType == 'nickName') {
      this.data.reqData["xgmember.nickName"]= v
    } else if (nodeType == 'phoneNo') {
      this.data.reqData["xgmember.phone"] = v
    } else if (nodeType == 'password') {
      this.data.reqData["xgmember.passWord"] = v
    } else if (nodeType == 'passwordre') {
      this.data.passwordre = v
    } else if (nodeType == 'idCardNo') {
      this.data.reqData["xgmember.idCard"] = v
    } else if (nodeType == 'invCode') {
      this.data.reqData["xgmember.code"] = v
    } else if (nodeType == 'vaCode') {
      this.data.reqData["xgmember.yzcode"] = v
      this.data.reqData.code=v;
    }
  },
  doSave: function (e) {
    var _this = this;
    console.log(this.data.reqData)
    let data = this.data.reqData
    let req = {};

    if (_this.data.passwordre.trim() != _this.data.reqData['xgmember.passWord'].trim()) {
      wx.showToast({
        title: '两次密码输入不一致！',
        icon: 'none'
      });
      return
    }
    if (_this.data.checkStatus == null || _this.data.checkStatus == '' || _this.data.checkStatus==undefined){
      wx.showToast({
        title: '请同意并接受《亲青恋用户注册协议》！',
        icon: 'none'
      });
      return
    }
    for (let k in data) {
      if (k =="xgmember.idCard"){
        if (!utils.idCardValid(data[k])){
          wx.showToast({
            title: '请输入正确的身份证号码！',
            icon: 'none'
          });
          return
        }
      } else if (k == "xgmember.code") {
         continue;
      }else if (data[k] == '' || data[k] == null || data[k] == undefined) {
        wx.showToast({
          title: '请检查带星号为必填项！',
          icon: 'none'
        });
        return
      }
    }
    _this.data.reqData.openId = wx.getStorageSync("openid");
    _this.data.reqData.unionid = wx.getStorageSync("unionid")
    app.doReq(app.globalData.SERVICE.USERCENTER.REGISTER, _this.data.reqData, 'get',
        function (res) {
          var data = res.data;
          if (data.code == 200 && data.type == 'SUCCESS') {
            wx.showToast({
              title: '注册成功',
              icon: 'success'
            });
            //注册成功后根据openid去查询用户基本信息，并且更新全局数据的userid
            app.getUserInfoSyn({"openId":wx.getStorageSync('openid')}).then(function(){
              var pages = getCurrentPages();
            var currentPage = pages[pages.length - 1];//当前页面
            var pervPagge = pages[pages.length - 2];//上个页面
            pervPagge.setData({
              addName: e.currentTarget.dataset.city + e.currentTarget.dataset.country,
            });
            wx.navigateBack({});
            })
          }else{
            wx.showToast({
              title: data.errmsg,
              icon:'none'
            })
          }
        });
  },
  doCancel: function () {
    wx.navigateBack({})
  },
  getVaCode: function () {
    var _this = this;
    var phoneNo = this.data.reqData["xgmember.phone"];
    if (this.data.smsAvailable) {
      this.setData({
        smsSeconds: 60,
        disabled: false,
      });
      if (phoneNo == undefined || phoneNo == null || phoneNo.trim() === '') {
        wx.showToast({
          title: '请输入手机号！',
          icon: 'none'
        })
        return
      } else {
        app.doReq(app.globalData.SERVICE.USERCENTER.SEND_CAPTCHA, { phone: phoneNo }, 'get', function (res) {
          if (res.data.code == 200) {
            wx.showToast({
              title: '发送成功',
              icon: 'success'
            });
          }
        });
        _this.setData({
          smsAvailable: false,
          disabled: true
        })
        var timer = setInterval(function () {
          if (_this.data.smsSeconds == 1) {
            _this.setData({
              smsAvailable: true,
              disabled: false
            })
            clearInterval(timer);
          } else {
            var sec = _this.data.smsSeconds;
            sec--
            _this.setData({
              smsSeconds: sec
            })
          }
        }, 1000);
      }
    }
  },
  goLis: function () {
    wx.navigateTo({
      url: '../../pages/webRegister/webRegister'
    })
  },
  agree: function (e) {
    console.log(e);
    this.data.checkStatus = e.detail.value[0];
    // var v = e.
    //  _this.data.reqData.checkStatus=1
  },
  goBind:function(){
    wx.navigateTo({
      url: '../../pages/accountBind/accountBind'
    })
  }
});
